var __extends = this && this.__extends || function (e, t) {
    for (var n in t) if (t.hasOwnProperty(n)) e[n] = t[n];
    function o() { this.constructor = e; }
    e.prototype = t === null ? Object.create(t) : (o.prototype = t.prototype, new o());
};

var devToolPort = null;
var contentPort = null;
var inspTabId = 0;

chrome.runtime.onMessage.addListener(function (e, t, n) {
    if (e.from == "devtools-page" && e.tabId) {
        inspTabId = e.tabId;
    }
    if (devToolPort && e.toDevTool) {
        devToolPort.postMessage(e);
    }
    if (e == "getTab") {
        n(t);
    }
});

var PortHandler = function () {
    function e(e) {
        this.port = e;

        e.onMessage.addListener(this.messageReceived);
        e.onDisconnect.addListener(this.disconnect);
    }
    e.prototype.messageReceived = function (e) {
        if (e.toContent) {
            if (contentPort) contentPort.postMessage(e);
        }
        if (e.toDevTool) {
            if (devToolPort) devToolPort.postMessage(e);
        }
    };
    e.prototype.postMessage = function (e) {
        this.port.postMessage(e);
    };
    e.prototype.disconnect = function (e) { };
    return e;
}();

var ContentPortHandler = function (e) {
    __extends(t, e);
    function t() {
        e.apply(this, arguments);
    }
    t.prototype.messageReceived = function (t) {
        e.prototype.messageReceived.call(this, t);
    };
    t.prototype.disconnect = function (e) {
        contentPort = null;
    };
    return t;
}(PortHandler);

var ToolPortHandler = function (e) {
    __extends(t, e);
    function t() {
        e.apply(this, arguments);
    }
    t.prototype.messageReceived = function (t) {
        e.prototype.messageReceived.call(this, t);
        if (t.open) {
            chrome.tabs.executeScript(inspTabId, { code: "startListen()" });
        }
    };
    t.prototype.disconnect = function (e) {
        contentPort = null;
    };
    return t;
}(PortHandler);

var PortServer = function () {
    function e() {
        this.conns = {
            stage: {},
            view: {},
            indexConnection: null
        };
        this.stageViewMapping = {};
        this.inspecters = {};
        this.views = {};
        this.viewOpened = false;
        this.indexConnection = null;
    }

    e.prototype.saveConnection = function (e, t) {
        var n = e.data.from;
        var o = e.key;
        var i = e.data.targetKey;
        if (i) i = decodeURIComponent(i);
        this.conns[o] = t;
        t["key"] = o;
        this.conns[n][o] = e.data.href;
        if (i) {
            this.stageViewMapping[i] = o;
            this.stageViewMapping[o] = i;
        }
        this.updateStageList();
    };

    e.prototype.updateStageList = function () {
        var e = this.conns.indexConnection;
        if (e && e["disconnected"]) {
            e = null;
            this.conns.indexConnection = null;
        }
        if (!e) return;
        e.postMessage({
            data: {
                name: "stageListUpdated",
                stages: this.conns.stage
            }
        });
    };

    e.prototype.createServer = function (e) {
        var t = this;
        if (e === void 0) { e = 3001; }

        chrome.runtime.onConnect.addListener(function (e) {
            e.onMessage.addListener(function (n) {
                n.key = decodeURIComponent(n.key);
                var o = n.key;

                if (n.data && n.data.name == "init") {
                    t.saveConnection(n, e);
                }

                if (n.data && n.data.type == "index") {
                    t.conns.indexConnection = e;
                    e.onDisconnect.addListener(function () {
                        return t.conns.indexConnection = null;
                    });
                    t.updateStageList();
                }

                if (n.data && n.data.name == "isDevToolOpen") {
                    var i = t.stageViewMapping[o];
                    var s = i && t.conns[i];
                    s = !!s;
                    e.postMessage({
                        id: n.id,
                        toContent: true,
                        data: s
                    });
                }

                if (o in t.stageViewMapping) {
                    var a = t.stageViewMapping[o];
                    var r = t.conns[a];
                    r && r.postMessage(n);
                }
            });

            e.onDisconnect.addListener(function (n, o) {
                var i = e["key"] || e.name;
                e["disconnected"] = true;
                var s = t.stageViewMapping[i];

                if (s) {
                    var a = t.conns[s];
                    a.postMessage({
                        data: {
                            name: "initOptions",
                            highlightClick: false,
                            highlightHover: false,
                            preventTouch: false,
                            showMethods: false,
                            showPrivate: true
                        },
                        key: i
                    });
                }

                delete t.conns[i];
                delete t.conns.stage[i];
                delete t.conns.view[i];
                t.updateStageList();
            });
        });
    };

    return e;
}();

(new PortServer).createServer();